import requests,json
from lxml import etree

class Wangyi(object):
    """爬取网易云音乐"""
    def __init__(self,url_dict):
        self.name,self.tree = self.get_html_tree(url_dict)
        self.parse()
        pass

    # 请求页面得到tree
    def get_html_tree(self,url_dict):
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
        }
        name = list(url_dict.keys())[0]
        url = list(url_dict.values())[0]
        response = requests.get(url,headers=headers).content.decode('utf-8')
        tree = etree.HTML(response)
        return name,tree


    # 解析页面
    def parse(self,):
        # print(tree)
        sing_type_list = self.tree.xpath('//div[@class="blk"]//li')
        for i in sing_type_list:
            sing_type_dict = dict()
            sing_type_name = i.xpath('./a/text()')[0].replace('/','_')  # 歌手类型名
            sing_type_url = 'https://music.163.com' + i.xpath('./a/@href')[0]   # 歌手类型url
            sing_type_dict[sing_type_name] = sing_type_url
            # print(sing_type_dict)
            # 第二次发起请求，得到分类歌手名单
            sing_type_name_1,sing_type_tree = self.get_html_tree(sing_type_dict)
            sing_char_list = sing_type_tree.xpath('.//ul[@id="initial-selector"]//li[position()>1]')
            sing_name_dict = dict()
            for j in sing_char_list:
                sing_char_dict = {}
                sing_char = j.xpath('./a/text()')[0]
                sing_char_url = j.xpath('./a/@href')[0]
                sing_char_dict[sing_char] = 'https://music.163.com' + sing_char_url
                print(sing_char_dict)
                # 第三次请求，得到每个字母下的歌手名字
                sing_char_name,sing_tree = self.get_html_tree(sing_char_dict)
                # print(sing_char_name)
                sing_name_list_li = sing_tree.xpath('//ul[@id="m-artist-box"]//li')
                # print(sing_name_list_li)
                sing_name_list = []
                for k in sing_name_list_li:
                    sing_name = k.xpath('.//p/a[1]/text()|./a[1]/text()')[0]
                    sing_name_list.append(sing_name)
                sing_name_dict[sing_char] = sing_name_list
                # print(sing_char_dict)
            with open('./歌手/'+sing_type_name+'.json','a',encoding='utf-8') as fp:
                json.dump(sing_name_dict,fp,ensure_ascii=False)
            # print(sing_type_name)


                # print(sing_char_dict)

            # print(sing_type_dict)
        pass


if __name__ == '__main__':
    url_dict = {'homepage':'https://music.163.com/discover/artist/cat?id=1001'}
    """赵秀广在9月15号添加了一句话"""
    """克隆到本地的仓库"""
    Wangyi(url_dict)